package com.greyshark.sanotrive.language

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.common.app.base.AppActivity
import com.common.app.core.RouterHub
import com.common.app.utls.HeadTitleUtils
import com.common.base.utils.LanguageType
import com.common.base.utils.MultiLanguageUtil
import com.common.base.utils.StatusBarUtils
import com.module.user.R
import com.greyshark.sanotrive.databinding.ActivitySetLanguageBinding
import com.greyshark.sanotrive.main.MainActivity

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2023/2/5
 *
 * 描述：
 *
 * 修订历史：
 *
 */
@Route(path = RouterHub.ROUTER_LANGUAGE_ACTIVITY)
class LanguageSettingActivity : AppActivity<ActivitySetLanguageBinding, LanguageSettingViewModel>(), View.OnClickListener {

    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    override fun getViewModel() = LanguageSettingViewModel::class.java

    override fun getViewContentBinding() = ActivitySetLanguageBinding.inflate(layoutInflater)

    private var savedLanguageType = 0


    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle(resources.getString(R.string.ac_set_language_title))
        headTitleUtils.setLineStyle(10)

//        mViewContentBinding.rlFollowsytem.setOnClickListener(this)
        mViewContentBinding.rlEnglish.setOnClickListener(this)
        mViewContentBinding.rlSimplifiedChinese.setOnClickListener(this)
        mViewContentBinding.rlTraditionalChinese.setOnClickListener(this)
    }

    override fun lazyInitData() {
        super.lazyInitData()
        MultiLanguageUtil.instance?.let {
            savedLanguageType = it.getLanguageType(this)
        }
        if (savedLanguageType == LanguageType.LANGUAGE_FOLLOW_SYSTEM) {
            setFollowSystemVisible()
        } else if (savedLanguageType == LanguageType.LANGUAGE_CHINESE_TRADITIONAL) {
            setTraditionalVisible()
        } else if (savedLanguageType == LanguageType.LANGUAGE_EN) {
            setEnglishVisible()
        } else if (savedLanguageType == LanguageType.LANGUAGE_CHINESE_SIMPLIFIED) {
            setSimplifiedVisible()
        } else {
            setSimplifiedVisible()
        }
    }

    override fun initEvents() {
        super.initEvents()
    }

    override fun onClick(v: View?) {

        when (v) {
//            mViewContentBinding.rlFollowsytem -> {
//                setFollowSystemVisible()
//                changeUI(LanguageType.LANGUAGE_FOLLOW_SYSTEM)
//            }
            mViewContentBinding.rlEnglish -> {
                setEnglishVisible()
                changeUI(LanguageType.LANGUAGE_EN)
//                showAlertDialogOneBtn(
//                    getString(com.module.pubpage.R.string.common_i_know_tip2), "",
//                    "国际版仅内测用户可体验", false
//                )
            }
            mViewContentBinding.rlSimplifiedChinese -> {
                setSimplifiedVisible()
                changeUI(LanguageType.LANGUAGE_CHINESE_SIMPLIFIED)
            }
            mViewContentBinding.rlTraditionalChinese -> {
                setTraditionalVisible()
                changeUI(LanguageType.LANGUAGE_CHINESE_TRADITIONAL)
            }
        }
    }

    private fun setSimplifiedVisible() {
//        mViewContentBinding.ivFollowsystem.visibility = View.GONE
        mViewContentBinding.ivEnglish.visibility = View.GONE
        mViewContentBinding.ivSimplifiedChinese.visibility = View.VISIBLE
        mViewContentBinding.ivTraditionalChinese.visibility = View.GONE
    }

    private fun setEnglishVisible() {
//        mViewContentBinding.ivFollowsystem.visibility = View.GONE
        mViewContentBinding.ivEnglish.visibility = View.VISIBLE
        mViewContentBinding.ivSimplifiedChinese.visibility = View.GONE
        mViewContentBinding.ivTraditionalChinese.visibility = View.GONE
    }

    private fun setTraditionalVisible() {
//        mViewContentBinding.ivFollowsystem.visibility = View.GONE
        mViewContentBinding.ivEnglish.visibility = View.GONE
        mViewContentBinding.ivSimplifiedChinese.visibility = View.GONE
        mViewContentBinding.ivTraditionalChinese.visibility = View.VISIBLE
    }

    private fun setFollowSystemVisible() {
//        mViewContentBinding.ivFollowsystem.visibility = View.VISIBLE
        mViewContentBinding.ivEnglish.visibility = View.GONE
        mViewContentBinding.ivSimplifiedChinese.visibility = View.GONE
        mViewContentBinding.ivTraditionalChinese.visibility = View.GONE
    }

    private fun changeUI(selectedLanguage: Int = 0) {
        MultiLanguageUtil.instance?.updateLanguage(this, selectedLanguage)
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK)
        startActivity(intent)
    }
}